package com.lbc.url;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class UrlDown {
    public static void main(String[] args) throws Exception {
        //  1.下载地址
//        URL url = new URL("https://m10.music.126.net/20201211163914/f20a63307a40ae33985c6ccdf67e6543/yyaac/0708/0652/0508/0b9b6827b718aa223af92bd52aa2424f.m4a");
//        URL url = new URL("https://42-49-113-146.mcdn.bilivideo.cn:480/upgcxcode/80/59/265135980/265135980_nb2-1-30032.m4s?expires=1607683601&platform=pc&ssig=7iZ-WAjOM5ZmWCjsM0wRIQ&oi=1999066250&trid=c66a2243e7b14704901efb745bf537a7u&nfc=1&nfb=maPYqpoel5MI3qOUX6YpRA==&mcdnid=1000040&mid=416588505&orderid=0,3&agrr=1&logo=A0000001");
        URL url = new URL("https://42-49-113-146.mcdn.bilivideo.cn:480/upgcxcode/80/59/265135980/265135980_nb2-1-30280.m4s?expires=1607683601&platform=pc&ssig=ByOrAq-cqNbjc36hMfk9Lg&oi=1999066250&trid=c66a2243e7b14704901efb745bf537a7u&nfc=1&nfb=maPYqpoel5MI3qOUX6YpRA==&mcdnid=1000040&mid=416588505&orderid=0,3&agrr=1&logo=A0000001");


        //  2.连接到这个资源  HTTP
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

        //  3.一切都是流
        InputStream inputStream = urlConnection.getInputStream();

//        FileOutputStream fos = new FileOutputStream("D:\\aaa\\music\\世间美好与你格格不入.m4a");
//        FileOutputStream fos = new FileOutputStream("D:\\aaa\\music\\手工耿.mp4");
        FileOutputStream fos = new FileOutputStream("D:\\aaa\\music\\手工耿.mp3");

        byte[] buffer = new byte[1024];
        int len;
        while ((len=inputStream.read(buffer))!=-1) {
            fos.write(buffer,0,len);  //  写出这个数据
        }

        fos.close();
        inputStream.close();
        urlConnection.disconnect();  //  断开连接
    }
}
